package com.ctim.service.user;

import com.ctim.common.exception.EnumPcsService;
import com.ctim.common.exception.PcsRunTimeException;
import com.ctim.common.pcs.PcsListResult;
import com.ctim.dao.user.ResourceRoleDao;
import com.ctim.dao.user.RoleDao;
import com.ctim.dao.user.UserRoleDao;
import com.ctim.model.user.Role;
import com.ctim.service.bean.RoleBean;
import com.ctim.service.config.TransactionalControl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * User: lb on 2016/9/6.
 * Date:2016-09-06-18:17
 * desc：
 */
@Service
public class RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private ResourceRoleDao resourceRoleDao;

    @Autowired
    private UserRoleDao userRoleDao;

    /**
     * 添加角色
     *
     * @param roleBean roleBean
     */
    @TransactionalControl
    public void addRole(RoleBean roleBean) {
        if (roleDao.selectSingleRole(roleBean.getName()) != null) {
            throw new PcsRunTimeException(EnumPcsService.BUSINESS_ROLE_EXISTED);
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleBean,role);
        roleDao.insertRole(role);
        String[] permissions = roleBean.getPermissions().split(",");
        resourceRoleDao.insertResourceRole(roleBean.getId(), permissions);
    }


    /**
     * 查找所有角色列表
     *
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public PcsListResult selectRoleList(String name, Integer pageIndex, Integer pageSize) {
        PcsListResult pcsListResult = new PcsListResult();
        Integer total;
        List<Role> list = new ArrayList<>();
        // 偏移量
        int pageOffset = pageSize * (pageIndex - 1);
        total = roleDao.countRole(name);
        if (total != null && total > 0) {
            list = roleDao.selectRoleList(name, pageOffset, pageSize);
        }
        pcsListResult.setData(list);
        pcsListResult.setTotal(total);
        return pcsListResult;
    }


    /**
     * 删除角色 如果角色下有用戶 則不允許刪除
     *
     * @param roleId roleId
     */
    public boolean deleteRole(Integer roleId,Integer operateUserId) {
        int userCount = roleDao.countUser(roleId);
        if (userCount>0) {
            throw new PcsRunTimeException(EnumPcsService.BUSINESS_ROLE_USER_EXISTED);
        }
        return roleDao.deleteRole(roleId, operateUserId) > 0;
    }

    /**
     * 修改角色  如果权限有修改则先删除该角色所有权限，然后再添加对应权限
     *
     * @param roleBean
     */

    @TransactionalControl
    public void updateRole(RoleBean roleBean) {
        Role role = roleDao.selectSingleRole(roleBean.getName());
        if (role != null && role.getId().intValue() != roleBean.getId().intValue()) {
            throw new PcsRunTimeException(EnumPcsService.BUSINESS_ROLE_EXISTED);
        }
        Role roleModel = new Role();
        BeanUtils.copyProperties(roleBean,roleModel);
        roleDao.updateRole(roleModel);
        if (roleBean.getPermissions() != null) {
            String[] permissions = roleBean.getPermissions().split(",");
            resourceRoleDao.updateResourceRole(roleBean.getId(), permissions);
        }
    }

    public List<Role> selectRoles(Integer userId) {
        return userRoleDao.selectRoles(userId);
    }
}
